package edu.geo.filters;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;



import edu.geo.containers.GeoJson;
import edu.geo.containers.Json;
import edu.geo.containers.geom.impl.LineSegment;
import edu.geo.containers.geom.impl.Point;
import edu.geo.containers.geomdomain.GeometricDomain;
import edu.geo.db.conn.DBConnection;
import edu.geo.db.conn.DBConnectionProvider;
import edu.geo.exception.GeoInfoException;
import edu.geo.protocol.Properties;
import edu.geo.service.IGeometricDomainServiceFacade;


public class FeatureProcess  extends GeoProcessDecorator {
	
	private static final Log LOGGER = LogFactory.getLog(FeatureProcess.class);
	
	private static final String GEODB = "geodb";
	private IGeometricDomainServiceFacade geomDomainService;
	
	
    
	private DBConnectionProvider dbConnectionProviderGeoDB = new DBConnection(GEODB);
	
	


	public FeatureProcess(IGeometricDomainServiceFacade geomDomainService) {
		super();
		this.geomDomainService = geomDomainService;
	}

    
	@Override
	public Json dispatchResult(Json requestParams)
    {
		Json jsonFeatures = null;
    int domainId =requestParams.getDomainId();
	Json feature = requestParams.getFeatures().get(0);
    String type =feature.getGeometry().getType();
	try {
    if("Point".equalsIgnoreCase(type))
	{
		Geometry geom=feature.getGeometry().toGeometry();
		 Point p1 = (Point) geom;

			this.geomDomainService.addPoint(domainId,p1);		
	}else
	if("LineString".equalsIgnoreCase(type))
	{
		Geometry geom=feature.getGeometry().toGeometry();
		LineSegment l1 = (LineSegment) geom;
		this.geomDomainService.addLineSegment(domainId,l1);
	}else
	{
	LOGGER.info("elemento no  permitido");
	}
	//this.geomDomainService.addPoint(domainId, point);
	GeometricDomain features = this.geomDomainService.retrieve(domainId);
	List<Point> points = features.getPoints();
	jsonFeatures= new Json(); 	
	jsonFeatures.setName("resultGeometry");
	jsonFeatures.setType("FeatureCollection");
    ArrayList<Json> featureList= new ArrayList<Json>();
	
	for(Point point:points)
	  {
		 Json jfeature = new Json();
		 jfeature.setType("Feature");
		 GeoJson geoJson= new GeoJson();
		 geoJson.setType("Point");
		 ArrayList<Double> coordinates = new ArrayList<Double>();
		 coordinates.add(point.getX());
		 coordinates.add(point.getY());
		 geoJson.setCoordinates(coordinates);
		 jfeature.setGeometry(geoJson);
		 Properties prp = new Properties();
		 prp.setId(point.getId()+"");
		 prp.setResult(""+point.getAsWKT());
		 jfeature.setProperties(prp);
		 featureList.add(jfeature);				
	  }
	 
	List<LineSegment> Lines = features.getLineSegments();
	
	 for(LineSegment line:Lines)
	  {
		  Json jfeature= new Json();
		  jfeature.setType("Feature");
			GeoJson geoJson= new GeoJson();
			geoJson.setType("LineString");
			ArrayList<ArrayList<Double>> coordinates = new ArrayList<ArrayList<Double>>();
			Coordinate[] coords = line.getCoordinates();
			int numCoords=coords.length;
			for(int i =0;i<numCoords;i++)
			{
				ArrayList<Double> coord= new ArrayList<Double>();
				coord.add(coords[i].x);
				coord.add(coords[i].y);
				coordinates.add(coord);
			}
			geoJson.setCoordinates(coordinates);
			jfeature.setGeometry(geoJson);
			Properties prp = new Properties();
			prp.setId(""+line.getId());
			prp.setResult(""+line.getAsWKT());
			jfeature.setProperties(prp);
			featureList.add(jfeature);			
	  }
	 jsonFeatures.setFeatures(featureList);
    
	
	} catch (GeoInfoException e) {
		// TODO Auto-generated catch block
		//todo lanzar json con el error;
		e.printStackTrace();
		
	}
    LOGGER.info("agregando elemento"+domainId+" tipo"+type);
	return jsonFeatures;
    }
   }
   
 